package com.guiji.quartz.domain;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONNull;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.guiji.quartz.config.ProxyConfig;
import com.guiji.quartz.task.DataSaveUtil;
import com.guiji.quartz.util.DelPicUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: wangxw
 * @date 2021/12/6 13:33
 */
public class SeekingalphaCom {
	private static final Logger log = LoggerFactory.getLogger(SeekingalphaCom.class);

	private static Set<String> tabs = new HashSet<>();

	static {
		tabs.add("us-economy");
		tabs.add("ipos");
		tabs.add("todays-market");
	}


	public static void mainMethod() throws Exception {
		HttpResponse response = null;
		String listUrl = null;

		for (String tab : tabs) {
			String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
			try {
				if (tab.equals("ipos")) {

					listUrl = "https://seekingalpha.com/api/v3/articles?cacheBuster=" + dateStr + "&filter[category]=market-outlook%3A%3Acommodities&filter[since]=0&filter[until]=0&include=author%2CprimaryTickers%2CsecondaryTickers&isMounting=true&page[size]=40&page[number]=1";

				} else if (tab.equals("us-economy")) {
					listUrl = "https://seekingalpha.com/api/v3/news?filter[category]=market-news%3A%3Aus-economy&filter[since]=0&filter[until]=0&include=author%2CprimaryTickers%2CsecondaryTickers&isMounting=true&page[size]=25&page[number]=1";
				} else if (tab.equals("todays-market")) {
					listUrl = "https://seekingalpha.com/api/v3/articles?cacheBuster=" + dateStr + "&filter[category]=market-outlook%3A%3Atodays-market&filter[since]=0&filter[until]=0&include=author%2CprimaryTickers%2CsecondaryTickers&isMounting=true&page[size]=40&page[number]=1";
				}


				response = HttpRequest.get(listUrl)
					.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
					.header("Accept-Encoding", "gzip, deflate")
					.setHttpProxy(ProxyConfig.url, ProxyConfig.ip)
					.setConnectionTimeout(30000)
					.execute();

			} catch (Exception e) {
				log.error("访问 https://seekingalpha.com/market-news/%s 失败", tab);
				return;
			}
			if (null != response && response.getStatus() == 200) {
				String resp = response.body();
				JSONObject jobj = JSONUtil.parseObj(resp);
				JSONArray data = (JSONArray) jobj.get("data");
				Set<String> resUrl = getUrl(data);
				if (null != resUrl && resUrl.size() > 0) {
					getContent(resUrl, tab);
				}

			} else {
				log.error("https://seekingalpha.com/market-news/ipos " + String.valueOf(response.getStatus()));
			}
		}

	}

	private static Set<String> getUrl(JSONArray data) throws InterruptedException {
		Set<String> ids = new HashSet<>();
		for (int k = 0; k < data.size(); k++) {
			try {
				JSONObject jobj = (JSONObject) data.get(k);
				String id = (String) jobj.get("id");

				if (null != id) {
					ids.add(id);
				}
			} catch (Exception e) {
				log.error("getList exception");
			}

		}
		return ids;
	}

	private static void getContent(Set<String> idStrs, String tab) throws InterruptedException {

		for (String idStr : idStrs) {
			Thread.sleep(1000);
			HttpResponse response = null;
			JSONObject data = null;
			String detailUrl = null;
			String category = null;
			try {
				if (tab.equals("ipos")) {
					category = "Seekingalpha-CommoditiesNews";
					detailUrl = "https://seekingalpha.com/api/v3/articles/" + idStr + "?include=author%2CprimaryTickers%2CsecondaryTickers%2CotherTags%2Cpresentations%2Cpresentations.slides%2Cauthor.authorResearch%2Cco_authors%2CpromotedService%2Csentiments";
				} else if (tab.equals("us-economy")) {
					category = "seekingalpha-Economy";
					detailUrl = "https://seekingalpha.com/api/v3/news/" + idStr + "?include=author%2CprimaryTickers.gics.sector%2CprimaryTickers.gics.sub_industry%2CsecondaryTickers.gics.sector%2CsecondaryTickers.gics.sub_industry%2CotherTags";
				} else if (tab.equals("todays-market")) {
					category = "seekingalpha-Market";
					detailUrl = "https://seekingalpha.com/api/v3/articles/" + idStr + "?include=author%2CprimaryTickers.gics.sector%2CprimaryTickers.gics.sub_industry%2CsecondaryTickers.gics.sector%2CsecondaryTickers.gics.sub_industry%2CotherTags%2Cpresentations%2Cpresentations.slides%2Cauthor.authorResearch%2Cco_authors%2CpromotedService%2Csentiments";
				}

				response = HttpRequest.get(detailUrl)
					.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
					.header("Accept-Encoding", "gzip, deflate")
					.setHttpProxy(ProxyConfig.url, ProxyConfig.ip)
					.setConnectionTimeout(30000)
					.execute();

				if (null != response && response.getStatus() == 200) {
					String resp = response.body();
					JSONObject jobj = JSONUtil.parseObj(resp);
					data = (JSONObject) jobj.get("data");


				} else {
					log.error("https://seekingalpha.com/market-news/%s  status error", tab);
				}

			} catch (Exception e) {
				log.error("https://seekingalpha.com/market-news/%s  exception", tab);
				return;
			}
			if (null != data) {
				String url = null;
				String title = null;
				String img = null;
				String content = null;
				List<String> newImgs = new ArrayList<>();
				List<Object> srcImgs = new ArrayList<>();
				JXDocument jxDocument;
				try {
					JSONObject attributes = (JSONObject) data.get("attributes");
					title = (String) attributes.get("title");
					content = (String) attributes.get("content");
					if (!(attributes.get("gettyImage") instanceof JSONNull)) {
						JSONObject gettyImage = (JSONObject) attributes.get("gettyImage");
						img = (String) gettyImage.get("medium");
					}
					JSONObject links = (JSONObject) data.get("links");
					String canonical = (String) links.get("canonical");
					url = canonical;
					jxDocument = new JXDocument(content);
					try {
						int imgSize = jxDocument.sel("//img/@src").size();
						if (imgSize > 0) {
							String temp = null;
							for (int k = 0; k < imgSize; k++) {
								temp = jxDocument.sel("//img/@src").get(k).toString();
								if (temp != null) {
									img = temp.strip();
									srcImgs.add(img);
								}
							}
						}
					} catch (Exception e) {
						log.error("可能没有图片");
						img = "";
					} finally {
						if (srcImgs.size() > 0) {
							newImgs = DelPicUtil.delPic(srcImgs);
						}
					}


					if (null != content && content.length() > 50000 || content.replaceAll("\\$|[\\s\\p{Zs}]", "").length() < 20 || newImgs.size() > 20) {
						continue;
					}

					content = content.replaceAll("</p>", "\\$\\$\\$").replaceAll("<figcaption.*?</figcaption>", "")
						.replaceAll("</ul>.*?\n", "\\$\\$\\$")
						.replaceAll("</li>.*?\n", "\\$\\$\\$")
						.replaceAll("\n", "").replaceAll("<div id=.*?</div>", " ")
						.replaceAll("<(.|\n)*?>", " ")
						.replace("Editor's Note:  The summary bullets for this article were chosen by Seeking Alpha editors.","");
					Date date = new Date();
					System.out.println("=======================================================");
					System.out.println("url: " + url);
					System.out.println("title: " + title);
					if (newImgs.size() > 0) {
						for (Object newImg : srcImgs) {
							System.out.println("    img:" + newImg.toString());
						}
					}
					System.out.println("content:" + content);
					System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
					String source = "https://seekingalpha.com/market-news/" + tab;
					if (tab.equals("todays-market")) {
						source = "https://seekingalpha.com/market-outlook/" + tab;
					}


					DataSaveUtil.saveData(title, content, newImgs, url, date, source, category);
					log.info(Thread.currentThread().getName() + "-----------------Seekingalpha----------------");
				} catch (Exception e) {
					System.out.println(e);
					continue;
				}
			}
		}
	}


//	public static void main(String[] args) throws Exception {
//		mainMethod();
//	}

}



